var express = require('express');
var router = express.Router();
// var ccap = require("ccap")

// 导入文件上传模块
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req,file,cb) {
        cb(null,'./public/images/avatar');
    },
    filename: function (req,file,cb) {
        var suffix = file.mimetype.split("/")[1];
        var namr = file.fieldname + '-'+Date.now()+'.'+suffix;
        cb(null,namr);
        req.arr="/images/avatar/"+namr;
    }
})
var upload = multer({storage: storage});

var userDAO = require("../bin/db/usersDAO.js")

// var captcha = ccap({
//   generate:function(){
//     var sourceStr = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
//     var text = "";
//     for (var i = 0; i < 4; i++) {
//       var index = Math.floor(Math.random() * sourceStr.length);
//       var ch = sourceStr[index];
//       text += ch;
//     }
//     return text;
//   }
// });
// app.get("/api/captcha",(req,res)=>{
//   var arr = captcha.get();
//   req.session.captcha = arr[0];
//   res.set("Content-Type","image/jpeg");
//   res.send(arr[1]);
// });

router.post("/regist",function(req,res){
  // console.log(req.body);
  userDAO.find({username:req.body.username})
  .then(data=>{
    if(data) {
      res.json({ err: 1, msg: "此用户名已被占用" });
    }
    else {
      var u = new userDAO(req.body);
      u.save(function (err) {
      res.json({ err: 0, msg: "注册成功" });
      });
    }
  });
});
router.post("/login",function(req,res){
  userDAO.findOne(req.body)
  .then(data=>{
    if(!data){
      res.json({err:1,msg:"用户不存在"});
    }else{
      req.session.sell = data;
      res.json({err:0,msg:"登录成功"});
    }
  })
})
// 查询登录状态
router.get("/islogin",(req,res)=>{
  if(req.session.sell){
    req.session.sell.a = true;
    res.json({err:0,data:req.session.sell});
  }else{
    res.json({err:1});
  }
});
// 退出登录
router.get("/exit",(req,res)=>{
  // console.log("退出登录");
  delete req.session.sell;
  req.session.sell.a = false;
  res.json({err:0});
});
// 获取当前数据
router.get("/updata",(req,res)=>{
  res.json({err:0,data:req.session.sell})
});
// 添加您个人资料
router.post("/tianjia",function(req,res){
  userDAO.findOne({_id:req.session.sell._id})
  .then(data=>{
    data.email = req.body.email;
    data.nickname = req.body.nickname;
    data.homepage = req.body.homepage;
    data.weibo = req.body.weibo;
    data.jieshao = req.body.jieshao;
    var d = userDAO(data)
    d.save()
    .then(d=>{
      req.session.user =d;
      res.json({err:0});
    })

    })
  });
  // 上传头像
  var fs = require("fs");
  router.post("/avatar",upload.single("avatar"),(req,res)=>{
    var aaaaaa = req.session.sell.avatar;
    req.session.sell.avatar = req.arr;

    userDAO.update({_id:req.session.sell._id},{$set:{
      avatar:req.arr
    }})
    .then(data=>{
      res.json({err:0,msg:"上传成功"});  
      if(aaaaaa){
        fs.unlinkSync("./public"+aaaaaa);
      }
      
    })
  });
  
  router.post("/details",(req,res)=>{
    //  console.log(req.body)
     userDAO.findOne(req.body)
     .then(data=>{
       res.json({err:0,data:data})
     })
  })
module.exports = router;
